/**
 * author: zhangwei
 * 角色新增、编辑
 */
var pageType = "add";

var main = {

    init: function() {
        this.initTable();
    },
    initTable: function() {
        layui.use(['form'], function() {
            var form = layui.form;

            //监听提交
            form.on('submit(formSubmit)', function(data) {
                var url = "/user/changePassword";
                var json = $("#taskForm").serialize();
                $.ajax({
                    type:"post",
                    data:json,
                    async:false,
                    url:url,
                    success:function(result) {
                        if (result.code == property.successCode) {
                            successMsg("修改成功,请重新登录");
                            $.ajax({
                                type: "POST",
                                url: "/logout",
                                success: function (result) {
                                    localStorage.removeItem("userInfo");
                                    // window.location.href='/';
                                    // layer.close(index);
                                }
                            });
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error:function(result) {
                        errorMsg("系统异常");
                    }
                });
                return false;
            });

            //监听重置
            $("#cancel").click(function () {
                    parent.$(".myRefresh").click();
                    layer.close(index);
                    return false;
            });

        });
    }
}
main.init();

/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData(id) {
    this.type = "edit";
    layui.use('form', function(){
        var form = layui.form;
        var index = parent.layer.getFrameIndex(window.name);
        var json = {"id":id};
        //加载数据
        $.ajax({
            type:"get",
            data:json,
            async:false,
            url:"/task/getOne",
            success:function(result) {
                if (result.code == property.successCode) {
                    setFormData(result.data);
                    // form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });
}

function loadJob() {
    $.ajax({
        type:"get",
        async:false,
        url:"/job/queryAllMyJob",
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data
                setJob(data)
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
}

function setJob(data) {
    htmlstr = ""
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item['id']+">"+item['jobName']+"</option>"
        htmlstr = htmlstr+str
    }
    $("#jobId").html(htmlstr)
}

/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    data.startTime = formatSimpleDate(data.startTime)
    data.endTime = formatSimpleDate(data.endTime)
    property.setForm($("#taskForm"),data);
}



